package cn.tedu._03weibo_test.controller;

import cn.tedu._03weibo_test.base.response.JsonResult;
import cn.tedu._03weibo_test.base.response.StatusCode;
import cn.tedu._03weibo_test.mapper.CommentMapper;
import cn.tedu._03weibo_test.pojo.dto.CommentAddParam;
import cn.tedu._03weibo_test.pojo.entity.Comment;
import cn.tedu._03weibo_test.pojo.vo.UserVO;
import cn.tedu._03weibo_test.pojo.vo.WeiboVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "3.评论模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation("发表评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insertComment(CommentAddParam commentAddParam, @ApiIgnore HttpSession session){
        log.debug("开始发表评论：");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        log.debug("UserVO="+userVO);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam,comment);
        comment.setCreateTime(new Date());
        comment.setUserId(userVO.getId());
        int num = commentMapper.insertComment(comment);
        System.out.println(comment);
        if (num == 0){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok(comment);
    }
    @GetMapping("selectByWeiboId")
    @ApiOperation("获取评论")
    @ApiOperationSupport(order = 20)
    public JsonResult selectById(Long weiboId){
        log.debug("根据微博ID查看评论：");
        List<WeiboVO> weiboVO = commentMapper.selectByComment(weiboId);
        if (weiboVO == null || weiboVO.isEmpty()){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok(weiboVO);
    }
}
